package adhoc.tel.fer.hr;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Toast;

/**
 * Screen with availability settings.
 */
public class Settings extends Activity implements View.OnClickListener {

	private Toast toast;
	private Button applyBtn;
	private RadioGroup radioGroup;
	
	/** Selected option.*/
	private String selected;
	
	/** User ID fetched from Facebook*/
	private String id;
	
	private SQLiteDatabase database;
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
    	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);    
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);

		applyBtn = (Button) Settings.this.findViewById(R.id.apply);
		radioGroup = (RadioGroup) Settings.this.findViewById(R.id.radioGroup);


		id = PreferencesHelper.getFacebookID();
		selected = PreferencesHelper.getState();
		
		
		Log.d("SELECTED", selected);
		
		if (selected.equals("3")) {
			radioGroup.check(R.id.online);
		} 
		else if (selected.equals("0")) {
			radioGroup.check(R.id.offline);
		} 
		else if (selected.equals("1")) {
			radioGroup.check(R.id.invisible);
		}
		else if (selected.equals("2")) {
			radioGroup.check(R.id.busy);
		}

		applyBtn.setOnClickListener(this);
		radioGroup.setOnCheckedChangeListener(new RadioGroupListener());
	}


	@Override
	public void onClick(View v) {
		// Save selected option
		database.execSQL("UPDATE user SET state=" + selected);
		
        try {
        	final JSONObject availability = new JSONObject();
        	availability.put("fb_id", id);
        	availability.put("avail", selected);
            Log.d("AVAIL", id + " " + selected + " " );
        	
            // Send selected option to main server.
            new Thread(new Runnable() {
				@Override
				public void run() {
					boolean isSent = Util.sendToServerByPost("avail", availability);
					
					// Alert user if option isn't send
					if (!isSent){
						Settings.this.runOnUiThread(new Runnable() {
							
							@Override
							public void run() {
								// Get instance of Vibrator from current Context
								Vibrator  v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
								// Vibrate for 300 milliseconds
								v.vibrate(300);
								Toast.makeText(getApplicationContext(), "Unsuccesfull communication with server", 2000).show();
								v = null;
							}
						});
					}
				}
			}).start();
		} 
		  catch (JSONException e) {
			e.printStackTrace();
		} 
	}

	private class RadioGroupListener implements
			RadioGroup.OnCheckedChangeListener {
		
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			switch (checkedId) {
			
				case R.id.offline:
					toast = Toast
							.makeText(
									getApplicationContext(),
									"You will not participate in this service at all!",
									3000);
					toast.show();
					selected = "0";
					break;
				case R.id.invisible:
					toast = Toast
							.makeText(
									getApplicationContext(),
									"You will get notifications, but you are invisible to your friends!",
									3000);
					toast.show();
					selected = "1";
					break;
					
				case R.id.busy:
					toast = Toast
							.makeText(
									getApplicationContext(),
									"You won't get notifications, but you are visible to your friends!",
									3000);
					toast.show();
					selected = "2";
					break;
				case R.id.online:
					toast = Toast
							.makeText(
									getApplicationContext(),
									"Available for notifications and visible to your friends!",
									3000);
					toast.show();
					selected = "3";
					break;
		}
			
	   }
			
	}

	@Override
	protected void onDestroy() {
		database.close();
		super.onDestroy();
	}
}
